package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.domain.SmsHomeRecommendSubject;
import com.itheima.service.SmsHomeRecommendSubjectService;
import com.itheima.util.PageBean;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class SmsHomeRecommendSubjectController {

    @DubboReference
    private SmsHomeRecommendSubjectService subjectService;

    @GetMapping("/home/recommendSubject/list")
    public R list(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        PageBean pageBean = subjectService.list(pageNum, pageSize, subjectName, recommendStatus);
        return R.success(pageBean);
    }

    @PostMapping("/home/recommendSubject/create")
    public R add(@RequestBody List<SmsHomeRecommendSubject> recommendSubjects) {
        int res = subjectService.create(recommendSubjects);
        if (res > 0) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @PostMapping("/home/recommendSubject/delete")
    public R delete(@RequestParam("ids") List<Long> ids) {
        int res = subjectService.delete(ids);
        if (res > 0) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @PostMapping("/home/recommendSubject/update/recommendStatus")
    public R updateStatus(@RequestParam("ids") List<Long> ids, Integer recommendStatus) {
        int res = subjectService.updateStatus(ids, recommendStatus);
        if (res > 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @PostMapping("/home/recommendSubject/update/sort/{id}")
    public R updateSort(Integer sort, Integer id) {
        int res = subjectService.updateSort(sort, id);
        if (res > 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @GetMapping("/home/Subject/list")
    public R list1(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        PageBean pageBean = subjectService.list(pageNum, pageSize, subjectName, recommendStatus);
        return R.success(pageBean);
    }
}